/*
* @(#)ListLookupFromParaDocAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.upload.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.core.DocumentValidateManager;
import com.dcivision.dms.core.RootOperationManager;
import com.dcivision.dms.core.RootRetrievalManager;
import com.dcivision.dms.dao.DmsDocumentDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.PermissionManager;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;

/**
 * <p>Class Name:       ListLookupFromParaDocAction.java    </p>
 * <p>Description:      The list action class for ListLookupFromParaDocAction.jsp</p>
 *
 *    @author           Tony Chen
 *    @company          DCIVision Limited
 *    @creation date    29/06/2004
 *    @version          $Revision: 1.6.2.1 $
 */

public class ListLookupFromParaDocAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.6.2.1 $";


  /**
   *  Constructor - Creates a new instance of ListArchiveAction and define the default listName.
   */
  public ListLookupFromParaDocAction() {
    super();
    this.setListName("dmsSelectSingleList");
    this.setListFunctionName("getDocumentWithVersionLabelList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  
  public String getMajorDAOClassName() {
    return("com.dcivision.dms.dao.DmsDocumentDAObject");
  }
        
  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS);
  }

  
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
  	SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    DmsDocumentDAObject dmsDocDAO = new DmsDocumentDAObject(sessionContainer, conn);
    ListLookupFromParaDocForm docSearchForm = (ListLookupFromParaDocForm) form;
    PermissionManager permissionManager = sessionContainer.getPermissionManager();
    DmsDocument tmpDmsDoc = null;
    if (permissionManager.hasAccessRight(SystemFunctionConstant.DMS_PERSONAL_FOLDER,"R")) {
      tmpDmsDoc = dmsDocDAO.getPersonalRootFolderByCreatorID(sessionContainer.getUserRecordID());
    } else if (permissionManager.hasAccessRight(SystemFunctionConstant.DMS_PUBLIC_FOLDER,"R")) {
      
      String docType = "U";
      tmpDmsDoc = dmsDocDAO.getDocumentByDocType(docType);
     }else {
      throw new ApplicationException("");
    }
    
    //barbin 2006-4-14
       	
  	if (!Utility.isEmpty(tmpDmsDoc) &&
    		(Utility.isEmpty(docSearchForm.getParentID()) || Utility.isEmpty(request.getParameter("parentID")) )
				){
    	docSearchForm.setParentID(tmpDmsDoc.getID().toString());
    	docSearchForm.setRootID(tmpDmsDoc.getRootID().toString());
    }else {
    	docSearchForm.setParentID(request.getParameter("parentID"));
    	docSearchForm.setRootID(request.getParameter("rootID"));
    }
  	
    request.setAttribute("rootID",docSearchForm.getRootID());
    request.setAttribute("parentID",docSearchForm.getParentID()); 
    if(!Utility.isEmpty(request.getParameter("selectedFilesValue"))){
    	request.setAttribute("selectedFilesValue",request.getParameter("selectedFilesValue"));
    }
    
    if(!Utility.isEmpty(request.getParameter("selectedFilesText"))){
    	request.setAttribute("selectedFilesText",request.getParameter("selectedFilesText"));
    }
   // log.debug("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
   // log.debug(request.getParameter("selectedFilesValue"));
   // log.debug(request.getParameter("selectedFilesText"));
    
    
    return super.execute(mapping,docSearchForm,request,response);
  }
  
  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    ListLookupFromParaDocForm docSearchForm = (ListLookupFromParaDocForm) form;
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    RootOperationManager rootOperationManager = new RootOperationManager(sessionContainer, conn);
    DmsDocument documentRoot = null;    
    
    try {
      // Retieve the root folder document object.
      documentRoot = docRetrievalManager.getRootFolderByRootID(TextUtility.parseIntegerObj(docSearchForm.getRootID()));
      request.setAttribute("rootFolder", documentRoot);

      // Retrieve the folder list for folder tree listing.
      List folderTreeList = docRetrievalManager.getFolderByParentIDRootID(documentRoot.getID(), new Integer(docSearchForm.getRootID()));
      request.setAttribute("folderTreeList", folderTreeList);

      // Retrieve the location path of the folder.
      Integer parentID = new Integer(docSearchForm.getParentID());
      String path = docRetrievalManager.getLocationPath(parentID);
      request.setAttribute("location", path);

      // Retrieve the accessible public root list.
      RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);
      List publicRootList = rootRetrievalManager.getAllAccessibleRootsList();
      request.setAttribute("publicRootList", publicRootList);

      // Call the super list function.
      DocumentValidateManager docValidateManager = new DocumentValidateManager(sessionContainer, conn);
      // check permission
      boolean havePermission = docValidateManager.checkHasPermissionOnObject(GlobalConstant.OBJECT_TYPE_DOCUMENT, TextUtility.parseIntegerObj(docSearchForm.getParentID()), TextUtility.parseIntegerObj(docSearchForm.getRootID()), "R");
      if (havePermission) {

        super.getListData(mapping, docSearchForm, request, response);

        List originalList = (ArrayList)request.getAttribute(this.m_sListName);
        List resultList = new ArrayList();

        for (int i = 0; i < originalList.size(); i++) {
          DmsDocument dmsDocument = (DmsDocument)originalList.get(i);
          List tmpList = docRetrievalManager.getVersionListByDocumentID(dmsDocument.getID());
          //dmsDocument.setVersionList(tmpList);
          resultList.add(tmpList);
        }
        request.setAttribute(this.m_sListName + "_versionList", resultList);
      }else{      	
        request.setAttribute(this.m_sListName + "_versionList", new ArrayList());
      }

      //this.commit(request);
      sessionContainer=null;
      docRetrievalManager.release();
      rootOperationManager.release();
      rootRetrievalManager.release();
      docValidateManager.release();

    } catch (ApplicationException appEx) {
      this.rollback(request);
      throw appEx;
    } catch (Exception e) {
      log.error(e, e);
      this.rollback(request);
      throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
    }finally{
      conn=null;
    }
  }
}
